<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form id="toolForm">
  <input type="radio" name="tool" checked="checked" value="pencil" id="pencil">画笔
  <br/>
  <input type="radio" name="tool" value="eraser" id="eraser">橡皮
</form>

<script>
  // 获取表单
  const toolForm=document.getElementById("toolForm");

  // 表单内选中的项发生改变时会调用该方法
  toolForm.onchange=function () {
    //这里得到name为tool的元素数组
    const tools = document.getElementsByName("tool");
    for (let i = 0; i <tools.length ; i++) {
      if(tools[i].checked)
        console.log(tools[i].value);
    }
  }

</script>
</body>
</html>
